﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Mysql_Planer;

namespace Planer
{
    public partial class Login : Form
    {
        public bool onlogin = false;

        public string onlevel = "0";

        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Txt_ID.Text;
            //Txt_PW.Text;
            
            string strConn = My_planer.sql_config;
            using (MySqlConnection conn = new MySqlConnection(strConn))
            {
                conn.Open();
                string sql = "SELECT UA_ID, UA_PW, UA_Name, UA_Group, UA_Level FROM user_account WHERE UA_ID =  @usrid ";

                string usrid = Txt_ID.Text;
                string usrpw = Txt_PW.Text;
                
                

                //ExecuteReader를 이용하여
                //연결 모드로 데이타 가져오기
                MySqlCommand cmd = new MySqlCommand(sql, conn);

                cmd.Parameters.Add("@usrid", MySqlDbType.VarChar, 7);
                cmd.Parameters[0].Value = usrid;

                
                
                MySqlDataReader rdr = cmd.ExecuteReader();
                
                if(rdr.Read())
                {
                    string sqlpw = rdr.GetString(1);
                    onlevel = rdr.GetString(4);
                    conn.Close();
                    if(sqlpw == usrpw)
                    {                      
                        onlogin = true;
                        MessageBox.Show("Login 하였습니다.", "로그인 성공", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        // 추가 소스 삽입해야됨. 버그있음
                        this.Close();      
                    }
                    else
                    {
                        onlogin = false;
                        MessageBox.Show("Password 가 틀렷습니다.", "비밀번호 틀림", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);   
                    }
                }
                else
                {
                    onlogin = false;
                    MessageBox.Show("등록되지 않은 유저입니다.", "사용자 없음", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);           
                }
                conn.Close();
                //while (rdr.Read())
                //{
                //    Console.WriteLine("{0}: {1}", rdr["Id"], rdr["Name"]);
                //}
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Register reg = new Register();
            reg.Show();
        }

        private void Txt_PW_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1_Click(sender, e);
            }
        }
    }
}